package jwdt.xadmin.bbs.action;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.bbs.bean.TBbbs;
import jwdt.xadmin.bbs.bean.TBbookmark;
import jwdt.xadmin.bbs.bean.TBuser;

import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller
public class UserAction extends BaseAction {
	
	
	@RequestMapping("/regAdd")
	public Page doRegAdd(Form f, Module m)throws Exception {
		String username = f.getString("username");
		String password = f.getString("password");
		int s = jdb.queryForInt("select count(*) from bbs_user where username='"+username+"'");
		//判断用户是否重复
		String ret = "注册成功，请重新登录";
		if(s == 0){
			TBuser obj = (TBuser)f.copy2Class(TBuser.class);
			obj.setUserpassword(StringUtil.MD5(password));
			try {
				obj.setUserclass(1);
				obj.setAdddate(DateUtil.getDateTime());
				jdb.add(obj);
				jdb.execute("update bbs_config set usernum=usernum+1,lastuser='"+username +"' where  id=1");
			} catch (Exception e) {
				e.printStackTrace();
				ret = "注册失败，请与管理员联系";
			}
		}else{
			ret="用户名已存在，请重新输入";
		}
		f.add("ret", ret);
		return m.findPage("tips");
	}
	
	@RequestMapping("/login")
	public Page doLogin(Form f, Module m)throws Exception {	
		String username=f.getString("username");
		String password=f.getString("userpassword");
		String sql = "select userid from bbs_user where username=? and userpassword=md5(?)";
		Object userid = jdb.uniqueResult(sql, new Object[]{username, password});
		if(userid == null) {
			f.add("ret", "用户名或密码错误，请重新输入！");
			return m.findPage("tips");
		}
		try {
			TBuser user = (TBuser)jdb.get(TBuser.class, userid);
			user.setLastlogin(DateUtil.getDateTime());
			f.setSession("bbsUserInfo", user);
			jdb.execute("update bbs_user set logins=logins+1,lastlogin='"+DateUtil.getDateTime()+"' where  username='"+username+"'");
		}catch(Exception e){
			e.printStackTrace();
		}
		//
		f.add("ret", "登录成功").add("url", "index.do");
		return m.findPage("tips");
	}
	
	@RequestMapping("/logout")
	public Page doLogout(Form f, Module m)throws Exception {
		f.setSession("bbsUserInfo", null);
		f.add("ret", "退出成功").add("url", "index.do");
		return m.findPage("tips");
	}
	
	
	@RequestMapping("/user")
	public Page doUser(Form f, Module m)throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo == null) {
			f.add("ret", "登录超时，请重新登录").add("url", "index.do");
			return m.findPage("tips");
		}
		String username = userInfo.getUsername();
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		String where = " where username='"+username+"' and parentid=0";
		String sql = super.select_bbsnews + where + super.order_bbsnews;
		PageUtil pu = new PageUtil("select count(*) from bbs_bbs1 "+where, sql, page, pageSize);
		f.add("pager", pu.setUrl(m.getFullPath() + ".do").setParams(f.buildQueryMap()));
		//f.add("reply",jdb.query("select distinct b.* from (select * from bbs_bbs1  where username='"+user+"' and parentid!=0 order by dateandtime desc) a left  join bbs_bbs1 b on a.parentid= b.announceid"));
		//f.add("bookmark",jdb.query("select b.* from bbs_bookmark a , bbs_bbs1 b where a.bbsid=b.announceid and a.username='"+user+"' order by a.addtime desc"));
		//f.add("praise",jdb.query("select b.* from bbs_praise a , bbs_bbs1 b where a.bid=b.announceid and a.username='"+user+"' order by a.addtime desc"));

		return m.findPage("/user");
	}
	
	@RequestMapping("/replyList")
	public Page doReplyList(Form f, Module m)throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		String username = userInfo.getUsername();
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		String where = " where username='"+username+"' and parentid!=0";
		String sql = "SELECT announceid, parentid, boardid, username,(SELECT topic FROM bbs_bbs1 a WHERE a.announceid=b.parentid) AS topic, dateandtime, hits, isbest, istop FROM bbs_bbs1 b" + where + super.order_bbsnews;
		PageUtil pu = new PageUtil("select count(*) from bbs_bbs1 "+where, sql, page, pageSize);
		return m.jsonPage(pu);
	}

	@RequestMapping("/favList")
	public Page doFavList(Form f, Module m)throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		String username = userInfo.getUsername();
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		String where = " where username='"+username+"'";
		String sql = "SELECT id, username, topic, addtime, bbsid FROM bbs_bookmark " + where + " order by addtime desc";
		PageUtil pu = new PageUtil("select count(*) from bbs_bookmark "+where, sql, page, pageSize);
		return m.jsonPage(pu);
	}

	@RequestMapping("/updateuser")
	public Page doUserUpdate(Form f, Module m)throws Exception {
		TBuser user = (TBuser)f.copy2Class(TBuser.class);
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		String ret="修改成功";
		try {
			jdb.execute("update bbs_user set title='"+user.getTitle()+"' , useremail='"+user.getUseremail()+"' where  username='"+userInfo.getUsername()+"'");
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		userInfo.setTitle(user.getTitle());
		userInfo.setUseremail(user.getUseremail());
		f.setSession("bbsUserInfo", userInfo);
		f.add("ret", ret);
		return m.getRedirectPage("/user.do");
	}	
	
	@RequestMapping("/favdel")
	public Page doFavdel(Form f, Module m)throws Exception {
		int id = f.getInt("id");
		TBbookmark obj = (TBbookmark)jdb.get(TBbookmark.class, id);
		if(obj == null) {
			f.add("ret", "您所取消收藏的帖子不存在");
			return m.findPage("tips");
		}
		String ret="取消收藏出错";
		try {
			jdb.del(obj);
			ret = "取消收藏成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return m.writePage(ret);
	}
	
	@RequestMapping("/favadd")
	public Page doFavadd(Form f, Module m)throws Exception {
		int id = f.getInt("id");
		TBbbs bbs = (TBbbs)jdb.get(TBbbs.class, id);
		if(bbs == null) {
			f.add("ret", "您所收藏的帖子不存在");
			return m.findPage("tips");
		}
		TBuser userInfo=(TBuser)f.getSession("bbsUserInfo");
		String username=userInfo.getUsername();
		String ret="收藏出错";
		TBbookmark obj = new TBbookmark();
		try {
			obj.setUsername(username);
			obj.setAddtime(DateUtil.getDateTime());
			obj.setBbsid(id);
			obj.setTopic(bbs.getTopic());
			jdb.add(obj);
			ret = "收藏成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		f.add("ret", ret).add("url", "/detail.do?id="+id);
		return m.findPage("tips");
	}

}
